﻿using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using ReportSystem_XY.Models;
using System.Configuration;
using ReportSystem_XY.Utility;
using System.Threading;
using System.Net.NetworkInformation;
using System.Drawing.Drawing2D;
using System.Linq;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
using ReportSystem_XY.SIP;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using ReportSystem_XY.WinCCArchive;
using ReportSystem_XY.BLL;

namespace ReportSystem_XY
{
    public partial class FrmMain : Form
    {

        public FrmMain()
        {
            InitializeComponent();
        }
        int IsFirst = 0; //是否是第一次加载页面
        int IsStart = 0;//倒计时
        string logtime = string.Empty;
       // String Source = "";
       // String SQLTime = "";
        String dbname = "";
        List<string> strList = new List<string>();
        private string appPath = Application.StartupPath;
        private void Form1_Load(object sender, EventArgs e)
        {
            dbname = XmlHelper.ReadXml("database")[0];
            btn_ReportSearch.Enabled = false;
            this.ReportViewer1.RefreshReport();
            tabControl.ItemSize = new Size(0, 1);
            tabControl.Appearance = TabAppearance.Buttons;
            tabControl.SizeMode = TabSizeMode.Fixed;
            textBox2.Text = "";
            textBox3.Text = "";
            textBox5.Text = "";

            ReportViewer1.Visible = false;
            DT_EndDate.MaxDate = DateTime.Today;
            DT_BeginDate.MaxDate = DT_EndDate.Value;
            DT_BeginTime.Value = Convert.ToDateTime("00:00:00");
            DT_EndTime.Value = DateTime.Now;
            ToolStripStatusLabel3.Text = "";
            ToolStripStatusLabel1.Text = DateTime.Now.ToString();
            //报表查询类型选择
            cmb_ReportType.DataSource = XmlHelper.ReadXml("reporttype");
            //采样时间初始化
            cmb_SampleTime.DataSource = XmlHelper.ReadXml("sampletime");
            cmb_SampleTime.SelectedIndex = 6;
            //   cmb_Device.DataSource = XmlHelper.ReadXml("device");
            // this.TopMost = true; //窗口置顶
            Action act = () =>
            {
                IsFirst = 1;
                if (this.Tag != null)
                {
                    UserInfos info = Tag as UserInfos;
                    lblUName.Text = info.UserName;
                    lblRoleName.Text = info.RoleName;
                    lblLogtime.Text = DateTime.Now.ToString();
                    logtime= DateTime.Now.ToString();                   
                }
                btn_ReportSearch.Enabled = false;
                pictureBox3.Show();
                ReportViewer1.Hide();
                tabPage2.Hide();
                tabPage3.Hide();
                tabPage1.Show();
                IsStart = 1;
            };
            act.TryCatch("主页面初始化出现异常");
        }
        private void InitMain()
        {
            Action act = () =>
            {
                if (this.Tag != null)
                {
                    UserInfos info = Tag as UserInfos;
                    lblUName.Text = info.UserName;
                    lblRoleName.Text = info.RoleName;
                    lblLogtime.Text= DateTime.Now.ToString();

                }

               

                btn_ReportSearch.Enabled = false;
                pictureBox3.Show();
                ReportViewer1.Hide();
                tabPage2.Hide();
                tabPage3.Hide();
                tabPage1.Show();
            };
            act.TryCatch("主页面初始化出现异常");
        }
        #region 菜单栏
        private void Timer1_Tick(object sender, EventArgs e)
        {
            ToolStripStatusLabel1.Text = DateTime.Now.ToString();

            //if(IsStart==1)
            //{
            //   if( (double) Convert.ToDateTime(ToolStripStatusLabel1.Text).Subtract(Convert.ToDateTime(logtime)).TotalSeconds==300.0)
            //    {
            //        IsStart = 0;
            //        Action act = () =>
            //        {
            //            foreach (Form frm in Application.OpenForms)
            //            {
            //                if (frm.Name == "FrmLogin")
            //                {
            //                    frm.Tag = new UserInfos()
            //                    {
            //                        UserName = string.Empty,
            //                        UserPwd = string.Empty,
            //                        RoleId = 101
            //                    };
            //                    this.Hide();
            //                    IsFirst = 2;
            //                    IsStart = 0;
            //                    frm.Show();
            //                    break;
            //                }

            //            }

            //        };
            //        act.TryCatch("登录系统出现异常");
            //    }
            //}
        }

        private void DateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            if (DT_EndDate.Value < DT_BeginDate.Value)
            {
                DT_EndDate.Value = DT_BeginDate.Value;
            }
        }
        private void DateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            DT_BeginDate.MaxDate = DT_EndDate.Value;
        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
          
                Application.ExitThread();
           
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }
        private void DateTimePicker3_ValueChanged(object sender, EventArgs e)
        {
            if (DT_EndTime.Value < DT_BeginTime.Value)
            {
                DT_EndTime.Value = DT_BeginTime.Value;
            }
        }

        private void DateTimePicker4_ValueChanged(object sender, EventArgs e)
        {
            DT_BeginTime.MaxDate = DT_EndTime.Value;
        }

        private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            btn_ReportSearch.Enabled = false;
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {

            btn_ReportSearch.Enabled = false;
            pictureBox3.Show();
            ReportViewer1.Hide();
            tabPage2.Hide();
            tabPage3.Hide();
            tabPage1.Show();
        }

        private void 数据库备份还原ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            listBox1.Items.Clear();
            tabPage1.Hide();
            tabPage3.Hide();
            tabPage2.Show();
        }
        String BackupsFile = "";
        String RestoreFile = "";


        public object Product_InfoTableAdapter { get; private set; }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog TargetAdd = new FolderBrowserDialog();
            TargetAdd.Description = "请选择数据库备份的目标位置";
            TargetAdd.RootFolder = Environment.SpecialFolder.MyComputer;
            TargetAdd.ShowNewFolderButton = false;
            if (TargetAdd.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = TargetAdd.SelectedPath;
                BackupsFile = textBox3.Text + "\\" + textBox2.Text + "_" + DateTime.Now.ToString("yyyyMMddhhmm") + ".bak";
                textBox4.Text = BackupsFile;
            }
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            BackupsFile = textBox3.Text + "\\" + textBox2.Text + "_" + DateTime.Now.ToString("yyyyMMddhhmm") + ".bak";
            textBox4.Text = BackupsFile;
        }
        /// <summary>
        /// 数据库备份
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            textBox4.Text = BackupsFile;
            if (textBox2.Text == "" || textBox3.Text == "")
            {
                MessageBox.Show("请填写正确的备份路径及文件名！", "提示框");
            }
            else
            {
                if (!File.Exists(BackupsFile))
                {

                    String Sql = "backup database " + dbname + " to disk='" + BackupsFile + "' with Init";
                    SQLHelper.ExecuteNonQuery(Sql);
                    MessageBox.Show("你已经成功备份当前数据！", "系统消息");
                }
                else
                {
                    MessageBox.Show("备份失败，该文件名已存在！", "提示框");
                }
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            /*
            OpenFileDialog BackupSource = new OpenFileDialog();
            BackupSource.Multiselect = false;
            BackupSource.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
            BackupSource.Filter="备份文件(*.bak)|*.bak";
            if (BackupSource.ShowDialog() == DialogResult.OK)
            {
                RestoreFile=BackupSource.FileName;
                textBox5.Text = BackupSource.FileName;
            }
            */
            FolderBrowserDialog TargetAdd = new FolderBrowserDialog();
            TargetAdd.Description = "请选择数据库备份所在的目录";
            TargetAdd.RootFolder = Environment.SpecialFolder.MyComputer;
            TargetAdd.ShowNewFolderButton = false;
            if (TargetAdd.ShowDialog() == DialogResult.OK)
            {
                listBox1.Items.Clear();
                listBox1.SelectedItem = null;
                textBox6.Text = "";
                textBox5.Text = TargetAdd.SelectedPath;
                String[] Files = Directory.GetFiles(textBox5.Text);
                foreach (string str in Files)
                    if (str.Substring(str.LastIndexOf('.') + 1).ToLower() == "bak")
                        listBox1.Items.Add(str);
            }
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                RestoreFile = listBox1.SelectedItem.ToString();
                textBox6.Text = RestoreFile;
            }
        }
        /// <summary>
        /// 数据库还原
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            if (textBox5.Text == "" || textBox6.Text == "")
            {
                MessageBox.Show("请选择正确的还原路径及文件名！", "提示框");
            }
            else
            {
                if (File.Exists(RestoreFile))
                {

                    String Sql = ("use master " +
                    "alter database " + dbname + " set offline with rollback immediate " +
                    "restore database " + dbname + " from disk = '" + RestoreFile + "' with Replace " +
                    "alter database " + dbname + " set online with rollback immediate");
                    SQLHelper.ExecuteNonQuery(Sql);
                    MessageBox.Show("你已经成功还原选定的数据！", "系统消息");

                }
                else
                {
                    MessageBox.Show("还原失败，该路径文件名不存在！", "提示框");
                }
            }
        }


        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Action act = () =>
            {
                foreach (Form frm in Application.OpenForms)
                {
                    if (frm.Name == "FrmLogin")
                    {
                        frm.Tag = new UserInfos()
                        {
                            UserName = string.Empty,
                            UserPwd = string.Empty,
                            RoleId = 101
                        };
                        this.Hide();
                        IsFirst = 2;
                        IsStart = 0;
                        frm.Show();
                        break;
                    }

                }

            };
            act.TryCatch("登录系统出现异常");
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            //FormUtility.CloseAllForm();
            if (MsgBoxHelper.MsgBoxConfirm("退出系统", "确定要退出系统?") == DialogResult.Yes)
            {
                Application.ExitThread();
            }
            else
                e.Cancel = true;


        }

        private void FrmMain_VisibleChanged(object sender, EventArgs e)
        {
            if (IsFirst == 2)
            {
                InitMain();
                IsFirst = 1;
                IsStart = 1;

            }
        }

        private void tabControl_MouseMove(object sender, MouseEventArgs e)
        {
            logtime = DateTime.Now.ToString();
        }

        private void pictureBox3_MouseMove(object sender, MouseEventArgs e)
        {
            logtime = DateTime.Now.ToString();
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            logtime = DateTime.Now.ToString();

        }

        private void ReportViewer1_MouseMove(object sender, MouseEventArgs e)
        {
            logtime = DateTime.Now.ToString();

        }

        private void 用户管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tabPage2.Hide();
            tabPage1.Hide();
            tabPage3.Show();
            string sql = "select a.*,b.RoleName from UserInfos a join RoleName b on a.RoleId=b.RoleId ";
            List<UserInfos> list = UserInfos.GetUserInfoListAll(sql);
            dgvUserList.DataSource = list;
        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            //接收信息

        }

        private void dgvUserList_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                UserInfos infos = this.Tag as UserInfos;
                if (infos != null)
                {

                    if (infos.RoleId == 1)
                    {
                        tsmAdd.Visible = true;
                        tsmEdit.Visible = false;
                    }
                    else
                    {
                        tsmAdd.Visible = false;
                        tsmEdit.Visible = false;
                    }
                }
                else
                {
                    tsmEdit.Visible = false;
                    tsmAdd.Visible = false;
                }

            }

        }

        private void dgvUserList_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {

            if (e.Button == MouseButtons.Right)
            {

                if (e.RowIndex > -1)
                {
                    dgvUserList.Rows[e.RowIndex].Selected = true;

                    UserInfos infos = this.Tag as UserInfos;
                    if (infos != null)
                    {
                        if (infos.RoleId > 0)
                        {
                            tsmEdit.Visible = true;
                            tsmAdd.Visible = false;
                        }
                        if (infos.RoleId == 1)
                        {
                            tsmAdd.Visible = true;
                        }
                    }
                    else
                    {
                        tsmEdit.Visible = false;
                        tsmAdd.Visible = false;
                    }
                }



            }
        }

        private void tsmAdd_Click(object sender, EventArgs e)
        {
            //转到主页面
            if (!FormUtility.CheckOpenForm("FrmSetUser"))
            {
                FrmSetUser fSetUser = new FrmSetUser();
                //登录页面显示处理---隐藏，不能关闭
                fSetUser.Tag = this.Tag;
                fSetUser.Show();
            }
        }

        #endregion
        /// <summary>
        /// 根据报表类型查询批号，分CIP,SIP两类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_BatchSearch_Click(object sender, EventArgs e)
        {
            int reportType;
            ReportNameSearchBLL reportNameSearchBLL = new ReportNameSearchBLL();
            reportType= cmb_ReportType.SelectedIndex;
            List<ReportNameModel> list = new List<ReportNameModel>();
            list =  reportNameSearchBLL.GetReportNameList(DT_BeginDate.Value.ToString("yyyy-MM-dd"), DT_BeginTime.Value.ToString("HH:mm:ss"), DT_EndDate.Value.ToString("yyyy-MM-dd"), DT_EndTime.Value.ToString("HH:mm:ss"), reportType);
            cmb_Batch.DataSource = list;
            cmb_Batch.DisplayMember = "reportname";
            if (list.Count > 0)
            {
                btn_ReportSearch.Enabled = true;
            }
            else
            {
                MessageBox.Show("没有查到批号信息");
            }
        }

        /// <summary>
        /// 根据前面查到的批号，查询报表的具体数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ReportSearch_Click(object sender, EventArgs e)
        {
            ToolStripStatusLabel3.Text = "数据查询中";
            List<ReportParameter> list = new List<ReportParameter>();
            list.Clear();
            int devicetype;
            string SampleTime = cmb_SampleTime.SelectedItem.ToString();
         
            ReportNameModel model = (ReportNameModel)cmb_Batch.SelectedItem;
            ReportDataBLL reportDataBLL = new ReportDataBLL();  
            //判断report的类型
            //SIP
            if (model.reporttype == 0)// SIP报表
            {
                devicetype = model.devicetype;
                //获取步骤信息
                DataTable dtstep = new DataTable();
                //步骤由两部分组成，一部分灭菌带小步骤信息+一部分大步骤信息
                dtstep = reportDataBLL.GetStepInfo(model);                             
                //灭菌流程开始和结束的时间
                string sipStart = dtstep.Compute("MIN(starttime)", "true").ToString();
                string sipStop = dtstep.Compute("MAX(stoptime)", "true").ToString();
                //获取灭菌步的开始和结束时间，判断时间间隔是否达标
                string sterStart = dtstep.Compute("min(starttime)", "processtype=2 and stepno=7").ToString();
                string sterStop = dtstep.Compute("max(stoptime)", "processtype=2 and stepno=7").ToString();
                //计算开始和结束的时间间隔 需要考虑只有吹扫步骤的模式
                if (sterStart == "" || sterStop == "")
                {
                    //MessageBox.Show("数据查询完成");
                    ToolStripStatusLabel3.Text = "数据查询完成";
                    string[] startdate = sipStart.Split(' ');
                    //往报表里面传递参数
                    list.Add(new ReportParameter("BatchString", model.reportname));
                    //  list.Add(new ReportParameter("ReportType", "SIP"));
                    list.Add(new ReportParameter("Temp", model.temp.ToString()));//灭菌温度
                    list.Add(new ReportParameter("PT", model.ptct.ToString()));//灭菌压力
                    list.Add(new ReportParameter("time", model.time.ToString()));//灭菌时间
                    if (model.username == "")
                    {
                        model.username = "admin2";
                    }
                    list.Add(new ReportParameter("Operator", model.username));//操作名称
                    list.Add(new ReportParameter("Date", Convert.ToDateTime(startdate[0]).ToString("yyyy-MM-dd")));//灭菌日期
                    list.Add(new ReportParameter("StartTime", Convert.ToDateTime(sipStart).ToString("yyyy-MM-dd HH:mm:ss")));//SIP开始时间
                    list.Add(new ReportParameter("StopTime", Convert.ToDateTime(sipStop).ToString("yyyy-MM-dd HH:mm:ss")));//SIP结束时间
                    ReportViewer1.Visible = true;
                    ReportViewer1.ProcessingMode = ProcessingMode.Local;
                    ReportViewer1.LocalReport.DataSources.Clear();
                    ReportViewer1.LocalReport.ReportPath = this.appPath + "\\ReportSIPA.rdlc";
                    ReportViewer1.LocalReport.SetParameters(list);
                    ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("dtStep", dtstep));
                    ReportViewer1.RefreshReport();
                }
                else
                {
                    if (XmlHelper.ReadXml("SIPTimeCheck")[0]== "true")
                    {
                        dtstep = reportDataBLL.GetStepAccuracy(dtstep, model);                  
                        sterStart = dtstep.Compute("min(starttime)", "processtype=2 and stepno=7").ToString();
                        sterStop = dtstep.Compute("max(stoptime)", "processtype=2 and stepno=7").ToString();                        
                    }
                   //获取整个灭菌过程的数据
                    string index = "92,93,94,95,96,97,98,99,100,101,102";
                    string cols = "TT1304,TT1311,TT2301,TT2302,TT2303,TT2305,TT2307,TT2308,TT2309,TT2310,TT2311";
                    DataTable dt_data = reportDataBLL.GetArchiveData(index,cols, sipStart, (Convert.ToDateTime(sterStop).AddSeconds(100)).ToString(), "1");
                    //获取灭菌段的详细数据
                    DataTable dt_Ster = reportDataBLL.GetArchiveData(index, cols, sterStart, (Convert.ToDateTime(sterStop).AddSeconds(1)).ToString(), SampleTime);  
                    //这两个索引值是用来给趋势图做划线用的，趋势图里面的两条竖线
                    int startIndex = (int)Convert.ToDateTime(sterStart).Subtract(Convert.ToDateTime(sipStart)).TotalSeconds - 2;
                    int stopIndex = startIndex + (int)model.time;
                    //MessageBox.Show("数据查询完成");
                    ToolStripStatusLabel3.Text = "数据查询完成";
                    string[] startdate = sipStart.Split(' ');
                    //往报表里面传递参数
                    list.Add(new ReportParameter("BatchString", model.reportname));
                    list.Add(new ReportParameter("Par0", startIndex.ToString()));//灭菌开始的索引
                    list.Add(new ReportParameter("Par1", stopIndex.ToString()));//灭菌结束的索引
                    list.Add(new ReportParameter("Par2", Convert.ToDateTime(sterStart).ToString("yyyy-MM-dd HH:mm:ss")));//灭菌开始的时间
                    list.Add(new ReportParameter("Par3", Convert.ToDateTime(sterStop).ToString("yyyy-MM-dd HH:mm:ss")));//灭菌结束的时间
                    list.Add(new ReportParameter("Temp", model.temp.ToString()));//灭菌温度
                    list.Add(new ReportParameter("PT", model.ptct.ToString()));//灭菌压力
                    list.Add(new ReportParameter("time", model.time.ToString()));//灭菌时间
                    if (model.username == "")
                    {
                        model.username = "admin";//默认用户名
                    }
                    list.Add(new ReportParameter("Operator", model.username));//操作名称
                    list.Add(new ReportParameter("Date", Convert.ToDateTime(startdate[0]).ToString("yyyy-MM-dd")));//灭菌日期
                    list.Add(new ReportParameter("StartTime", Convert.ToDateTime(sipStart).ToString("yyyy-MM-dd HH:mm:ss")));//SIP开始时间
                    list.Add(new ReportParameter("StopTime", Convert.ToDateTime(sipStop).ToString("yyyy-MM-dd HH:mm:ss")));//SIP结束时间
                    list.Add(new ReportParameter("DetailDisplay", (this.checkBox1.Checked == true) ? "true" : "false"));
                    ReportViewer1.Visible = true;
                    ReportViewer1.ProcessingMode = ProcessingMode.Local;
                    ReportViewer1.LocalReport.DataSources.Clear();
                    ReportViewer1.LocalReport.ReportPath = this.appPath + "\\报表模板\\ReportSIPB2.rdlc";//这个是横屏的模板
                    ReportViewer1.LocalReport.SetParameters(list);
                    ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("SIP_SET", dt_data));
                    ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("dtStep", dtstep));
                    ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("SIPDetail", dt_Ster));
                    ReportViewer1.RefreshReport();
                }
            }
            //判断report的类型
            //CIP
            if (model.reporttype == 1)// CIP报表
            {
                //获取步骤信息
                DataTable dtstep = new DataTable();               
                dtstep = reportDataBLL.GetStepInfo(model);
                //灭菌流程开始和结束的时间
                string cipStart = dtstep.Compute("MIN(starttime)", "true").ToString();
                string cipStop = dtstep.Compute("MAX(stoptime)", "true").ToString();
                //获取灭菌步的开始和结束时间，判断时间间隔是否达标
                string ctCheckStart = dtstep.Compute("max(starttime)", "processtype=5 and stepno=61").ToString();
                string ctCheckStop = dtstep.Compute("max(stoptime)", "processtype=5  and stepno=67").ToString();                
                //计算开始和结束的时间间隔 需要考虑只有吹扫步骤的模式
                if (ctCheckStart == "" || ctCheckStop == "")
                {
                    //MessageBox.Show("数据查询完成");                   
                    string[] startdate = cipStart.Split(' ');
                    //往报表里面传递参数
                    list.Add(new ReportParameter("BatchString", model.reportname));
                    list.Add(new ReportParameter("ReportType", "CIP"));
                    if (model.username == "")
                    {
                        model.username = "admin";
                    }
                    list.Add(new ReportParameter("Operator", model.username));//操作名称
                    list.Add(new ReportParameter("Date", Convert.ToDateTime(startdate[0]).ToString("yyyy-MM-dd")));//灭菌日期                                                                                                              
                    list.Add(new ReportParameter("StartTime", Convert.ToDateTime(cipStart).ToString("yyyy-MM-dd HH:mm:ss")));//SIP开始时间
                    list.Add(new ReportParameter("StopTime", Convert.ToDateTime(cipStop).ToString("yyyy-MM-dd HH:mm:ss")));//SIP结束时间
                   // list.Add(new ReportParameter("CTResult", "1"));
                    ReportViewer1.Visible = true;
                    ReportViewer1.ProcessingMode = ProcessingMode.Local;
                    ReportViewer1.LocalReport.DataSources.Clear();
                    ReportViewer1.LocalReport.ReportPath = this.appPath + "\\报表模板\\ReportCIPC.rdlc";
                    ReportViewer1.LocalReport.SetParameters(list);
                    ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("dtStep", dtstep));
                    ReportViewer1.RefreshReport();
                }
                else
                {
                    if (dtstep.Compute("MIN(starttime)", "processtype=5 and stepno=61").ToString() != dtstep.Compute("max(starttime)", "processtype=5 and stepno=61").ToString())
                    {
                        //说明电导检测的时候，第一次没过，是否需要调整时间，调整思路把前面的processtype5改成4？
                    }
                    //获取整个CIP过程的数据
                    string index = "85,86";
                    string cols = "CT1011,CT2011";
                    bool result;
                    DataTable dt_data = reportDataBLL.GetCTArchiveData(index, cols, ctCheckStart, (Convert.ToDateTime(ctCheckStart).AddSeconds(100)).ToString(),out result, "1");
                    ////这两个索引值是用来给趋势图做划线用的，趋势图里面的两条竖线
                    int startIndex = (int)Convert.ToDateTime(ctCheckStart).Subtract(Convert.ToDateTime(cipStart)).TotalSeconds - 2;
                    int stopIndex = (int)Convert.ToDateTime(ctCheckStop).Subtract(Convert.ToDateTime(cipStart)).TotalSeconds + 2;
                    ////MessageBox.Show("数据查询完成");
                    ToolStripStatusLabel3.Text = "数据查询完成";
                    string[] startdate = cipStart.Split(' ');
                    //往报表里面传递参数
                    list.Add(new ReportParameter("BatchString", model.reportname));
                      list.Add(new ReportParameter("ReportType", "CIP"));
                    if (model.username == "")
                    {
                        model.username = "admin";
                    }
                    list.Add(new ReportParameter("Operator", model.username));//操作名称
                    list.Add(new ReportParameter("Date", Convert.ToDateTime(startdate[0]).ToString("yyyy-MM-dd")));//灭菌日期                                                                                                                   
                    list.Add(new ReportParameter("StartTime", Convert.ToDateTime(cipStart).ToString("yyyy-MM-dd HH:mm:ss")));//SIP开始时间
                    list.Add(new ReportParameter("StopTime", Convert.ToDateTime(cipStop).ToString("yyyy-MM-dd HH:mm:ss")));//SIP结束时间
                    list.Add(new ReportParameter("CTResult", result? "true" : "false"));
                    ReportViewer1.Visible = true;
                    ReportViewer1.ProcessingMode = ProcessingMode.Local;
                    ReportViewer1.LocalReport.DataSources.Clear();
                    ReportViewer1.LocalReport.ReportPath = this.appPath + "\\报表模板\\ReportCIPA.rdlc";
                    ReportViewer1.LocalReport.SetParameters(list);
                   // ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("SIP_SET", dt_data));
                    ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("dtStep", dtstep));
                    ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("CIPDetail", dt_data));
                    ReportViewer1.RefreshReport();
                }
                ToolStripStatusLabel3.Text = "数据查询完成";
            }
            //判断report的类型
            //PROD
            if (model.reporttype == 2)
            {
                DataTable dtstep = reportDataBLL.GetStepInfo(model);
                
                string proStartTime = dtstep.Compute("min(starttime)", "true").ToString();
                string proStopTime = dtstep.Compute("max(stoptime)", "true").ToString();
                string[] startdate = proStartTime.Split(' ');


                string index = "87,88,92";
                string cols = "WT1001,PT1301,TT1304";
                DataTable dt_data = reportDataBLL.GetArchiveData(index, cols, proStartTime, proStopTime,"1");
                //往报表里面传递参数
                list.Add(new ReportParameter("BatchString", model.reportname));
                if (model.username == "")
                {
                    model.username = "admin";
                }
                list.Add(new ReportParameter("Operator", model.username));//操作名称
                list.Add(new ReportParameter("Date", Convert.ToDateTime(startdate[0]).ToString("yyyy-MM-dd")));//灭菌日期
                list.Add(new ReportParameter("StartTime", Convert.ToDateTime(proStartTime).ToString("yyyy-MM-dd HH:mm:ss")));//SIP开始时间
                list.Add(new ReportParameter("StopTime", Convert.ToDateTime(proStopTime).ToString("yyyy-MM-dd HH:mm:ss")));//SIP结束时间
                ReportViewer1.Visible = true;
                ReportViewer1.ProcessingMode = ProcessingMode.Local;
                ReportViewer1.LocalReport.DataSources.Clear();
                ReportViewer1.LocalReport.ReportPath = this.appPath + "\\报表模板\\Prod1.rdlc";
                ReportViewer1.LocalReport.SetParameters(list);
                ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("prodData", dtstep));
                ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("prodChartData", dt_data));
                ReportViewer1.RefreshReport();
            }
        }
        private void cmb_ReportType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmb_ReportType.SelectedItem.ToString() == "灭菌")
            {
                this.checkBox1.Visible = true;
            }
            else
            {
                this.checkBox1.Visible = false;
                this.checkBox1.Checked = false;
            }
        }              
    }
}
